package com.tobeface.wechat.message.handler;

import com.google.common.base.Objects;
import com.tobeface.wechat.message.WeChatMessage;
import com.tobeface.wechat.message.WeChatMessageChannel;
import com.tobeface.wechat.message.WeChatMessageEventTypes;
import com.tobeface.wechat.message.WeChatMessagePayloads;

/**
 * @author loudyn
 */
public abstract class ScanEventMessageHandlerSupport extends EventMessageHandlerSupport {

    @Override
    protected final boolean isEventMessageSupport(WeChatMessage message, String event) {
        if (!Objects.equal(WeChatMessageEventTypes.SCAN, event)) {
            return false;
        }

        String ticket = (String) message.getPayloads().get(WeChatMessagePayloads.EVENT_TICKET_PAYLOAD);
        String eventKey = (String) message.getPayloads().get(WeChatMessagePayloads.EVENTKEY_PAYLOAD);
        return isScanEventMessageSupport(message, ticket, eventKey);
    }

    /**
     * @param message
     * @param ticket
     * @param eventKey
     * @return
     */
    protected boolean isScanEventMessageSupport(WeChatMessage message, String ticket, String eventKey) {
        return true;
    }

    @Override
    protected final void onEventMessageReceived(WeChatMessageChannel channel, WeChatMessage message, String event) {
        String ticket = (String) message.getPayloads().get(WeChatMessagePayloads.EVENT_TICKET_PAYLOAD);
        String eventKey = (String) message.getPayloads().get(WeChatMessagePayloads.EVENTKEY_PAYLOAD);
        onMenuEventMessageReceived(channel, message, ticket, eventKey);
    }

    /**
     * @param channel
     * @param message
     * @param ticket
     * @param eventKey
     */
    protected abstract void onMenuEventMessageReceived(WeChatMessageChannel channel, WeChatMessage message,
                                                       String ticket, String eventKey);

}
